package com.jack.payment.rabbitmq.config;

import com.jack.payment.rabbitmq.constant.RabbitmqConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    private Logger log = LoggerFactory.getLogger(RabbitMQConfig.class);

    @Autowired
    private AmqpAdmin rabbitAdmin;

    /**
     * 设置rabbit mq 交换机类型为延迟
     *
     * @param exchangeType 交换机类型 topic, direct, fanout, headers,
     * @return
     */
    private Map<String, Object> getDelayedExchangeType(String exchangeType) {
        Map<String, Object> exchangeMap = new HashMap<>();
        exchangeMap.put("x-delayed-type", exchangeType);
        return exchangeMap;
    }

    /**
     * 初始rabbit mq 消息中间中的队列和消息交换机之间绑定,创建
     */
    @Bean
    public void createBindMQ() {
        bindingExchangeCommon();
    }

    public void bindingDelayExchange() {
        // 自定义队列交换机类型 为延迟   durable 是否为持久,为true 服务器重启时不会丢失相关信息
        CustomExchange exchange = new CustomExchange(
                RabbitmqConstant.EXCHANGE_DELAYED,
                "x-delayed-message",
                true,
                false, getDelayedExchangeType("topic"));
        Queue delayQueue = new Queue(RabbitmqConstant.QUEUE_PAYMENT_DELAY);
        rabbitAdmin.declareQueue(delayQueue);
        rabbitAdmin.declareExchange(exchange);
        rabbitAdmin.declareBinding(BindingBuilder.bind(delayQueue).to(exchange).with(RabbitmqConstant.ROUTING_DELAY_PAYMENT).noargs());
    }


    /**
     * 定义普通队列 与 topic 交换机
     */
    public void bindingExchangeCommon() {
        if (rabbitAdmin.getQueueProperties(RabbitmqConstant.EXCHANGE_COMMON) == null) {
            log.info("在Rabbitmq中未发现此交换机, 正在进行创建");
            // topic  交换机
            TopicExchange topicExchange = new TopicExchange(RabbitmqConstant.EXCHANGE_COMMON);

            // 声明基础队列名称
            Queue logQueue = new Queue(RabbitmqConstant.QUEUE_PAYMENT_LOGGER);
            Queue paymentQueue = new Queue(RabbitmqConstant.QUEUE_PAYMENT_COMMON);
            Queue refundQueue = new Queue(RabbitmqConstant.QUEUE_PAYMENT_REFUND);
            Queue loginQueue = new Queue(RabbitmqConstant.QUEUE_LOGIN);

            // 构建基础队列 与 交换机
            rabbitAdmin.declareQueue(logQueue);
            rabbitAdmin.declareQueue(paymentQueue);
            rabbitAdmin.declareQueue(refundQueue);
            rabbitAdmin.declareQueue(loginQueue);

            rabbitAdmin.declareExchange(topicExchange);
            // 交换机 与 队列进行绑定
            rabbitAdmin.declareBinding(BindingBuilder.bind(logQueue).to(topicExchange).with(RabbitmqConstant.ROUTING_LOGGER));
            rabbitAdmin.declareBinding(BindingBuilder.bind(paymentQueue).to(topicExchange).with(RabbitmqConstant.ROUTING_PAYMENT_CALL));
            rabbitAdmin.declareBinding(BindingBuilder.bind(refundQueue).to(topicExchange).with(RabbitmqConstant.ROUTING_REFUND));
            rabbitAdmin.declareBinding(BindingBuilder.bind(loginQueue).to(topicExchange).with(RabbitmqConstant.ROUTING_LOGIN));
        }
    }
}
